import { defineStore } from 'pinia';
import { useStorage } from '@vueuse/core'; // 使用 vueuse 的 useStorage 来持久化状态

// 定义 Pinia Store
export const useLevelStore = defineStore('level', () => {
  // 使用 useStorage 将数据持久化到 localStorage
  const pid = useStorage<string | null>('level-pid', null); // 持久化的 pid
  const ppid = useStorage<string | null>('level-ppid', null); // 持久化的 ppid
  const pppid = useStorage<string | null>('level-pppid', null); // 持久化的 pppid
  const moveid = useStorage<string | null>('level-moveid', null); // 持久化的 moveid

  // Actions
  const setPid = (newPid: string | null) => {
    pid.value = newPid;
  };

  const setPpid = (newPpid: string | null) => {
    ppid.value = newPpid;
  };

  const setPppid = (newPppid: string | null) => {
    pppid.value = newPppid;
  };

  const setMoveid = (newMoveid: string | null) => {
    moveid.value = newMoveid;
  };

  const clearAllLevels = () => {
    pid.value = null;
    ppid.value = null;
    pppid.value = null;
    moveid.value = null;
  };

  return {
    pid,
    ppid,
    pppid,
    moveid,
    setPid,
    setPpid,
    setPppid,
    setMoveid,
    clearAllLevels,
  };
});
